package com.example.practice20240802;

import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.example.practice20240802.databinding.ActivityHandlerTestBinding;

import org.w3c.dom.Text;

public class HandlerTest extends AppCompatActivity {
    private static final String TAG = "HandlerTest";
    private ActivityHandlerTestBinding binding;
    //    Handler handler = new Handler(Looper.getMainLooper());
    Button button;
    private MyViewModel viewModel;
    private SharedPreferences sp;

    @Override
    protected void onStart() {
        super.onStart();
        sp = getSharedPreferences("test", MODE_PRIVATE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("count", viewModel.getCount());
        edit.apply();
    }

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
//        getLifecycle().addObserver(new MyObserver());
        getLifecycle().addObserver(new MyActivityLifecycleObserver());


//        binding = ActivityHandlerTestBinding.inflate(getLayoutInflater());
        setContentView(R.layout.activity_handler_test);
//        binding.button.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                fun();
//
//            }
//        });

        //那去存入的值
        int count = 0;
        if (sp != null) {
            count = sp.getInt("count", 0);
        }

        viewModel = new ViewModelProvider(this, new MyViewModelFactory(count)).get(MyViewModel.class);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setCount(viewModel.getCount() + 1);
                refersh();
            }
        });
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               TextView textView= (TextView)findViewById(R.id.textView);
               viewModel.setCount(0);
               refersh();
            }
        });
        refersh();


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        sp = getSharedPreferences("test", MODE_PRIVATE);
        if (sp != null) {
            count = sp.getInt("count", 0);
        }

        viewModel = new MyViewModelFactory(count).create(MyViewModel.class);

        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.setCount(viewModel.getCount() + 1);
                refersh();
            }
        });
        refersh();


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }


    private void refersh() {
        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText("点击了" + viewModel.getCount() + "次");
    }

//    private void fun() {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                try {
//                    Thread.sleep(3000);
//                    handler.post(() -> button.setText("我是一个屌丝!!!"));
//                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }).start();


//    }

}